angular.module('enrollfromcontrollers',[])
  //我要报名的Controller
  .controller('SignInCtrl',function($scope,$state,$stateParams,Member,Meeting,Loading,ENV,Storage,Toast,History){

    var mmId = $stateParams.mmId;//获取到约局编号
    var memberId = Storage.get('member').member_id;

    $scope.signInMember = {
      username:'',
      telephone:'',
      weixin:''
    };
    $scope.goback = function(){
      History.goback();
    }
    Loading.showLoading();
    Member.getmemberInfo(memberId);
    $scope.$on('Member.MemberInfoUpdated',function(){
        var result = Member.getMemberInfoJson();
        $scope.signInMember.telephone = result.member_contact;
        $scope.signInMember.username = result.member_name;
        Loading.hideLoading();
    });
    //报名该约局
    $scope.submitSignIn = function(){
      if($scope.signInMember.username==''){
        Toast.showMessage("请填写真实姓名");
      }else if($scope.signInMember.telephone==''){
        Toast.showMessage("请填写联系方式");
      }else{
        Meeting.signInActivity(memberId,mmId,$scope.signInMember.username,$scope.signInMember.telephone,$scope.signInMember.weixin);
      }
    }
    $scope.$on('Meeting.signInActivityUpdated',function(){
      var result = Meeting.getSignInJson();
      if(result.signInState){//报名成功
        $state.transitionTo('enroll-from-success');
        //清空数据
        $scope.signInMember = {
          username:'',
          telephone:'',
          weixin:''
        };
      }else{//报名不成功
          Toast.showMessage(result.signInMsg);
      }
    });
  })
//报名成功的Controller
  .controller('SignInSuccessCtrl',function($scope,$ionicHistory,$ionicActionSheet,$cordovaFileTransfer,Meeting,ENV,Toast){
        $scope.meetingCodeImg = Meeting.getMeetingCodeImg();
        $scope.picPath = ENV.picPath;
        Meeting.setMeetingCodeImg('');
        $scope.goback = function(){
          $ionicHistory.goBack(-2);
        }
    //长按保存图片
    $scope.downloadImg = function(){
      var timestamp=new Date().getTime();
      var url = ENV.picPath+"big_"+$scope.meetingCodeImg;
      var targetPath = cordova.file.externalRootDirectory +"winelife/winelife_discover"+timestamp+".jpg";
      var trustHosts = true;
      var options = {};
      $ionicActionSheet.show({
        buttons: [
          {text: '保存到手机相册'}
        ],
        cancelText: '取消',
        buttonClicked: function (index) {
          if(index==0){
            $cordovaFileTransfer.download(url, targetPath, options, trustHosts)
              .then(function(result) {
                Toast.showMessage("保存成功");
              }, function(err) {
                Toast.showMessage("保存成功");
              }, function (progress) {
              });
          }
          return true;
        }
      });
    }

  })
